package com.opennetexam.manager.socketfactory.impl;

import com.opennetexam.manager.common.OperationResult;
import com.opennetexam.manager.databasemodel.ExamPhoto;
import com.opennetexam.manager.databasemodel.Examinee;
import com.opennetexam.manager.enums.OperationResultType;
import com.opennetexam.manager.enums.PhotoResourceType;
import com.opennetexam.manager.facecompare.FaceCompareByFile;
import com.opennetexam.manager.mapper.ExamPhotoMapper;
import com.opennetexam.manager.mapper.ExamineeMapper;
import com.opennetexam.manager.socketfactory.SocketServerBusinessProcess;
import com.opennetexam.manager.utils.DateUtil;
import com.opennetexam.manager.utils.ExamSitePath;
import com.opennetexam.manager.utils.FileUtil;
import com.opennetexam.manager.utils.GsonUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.Map;
import java.util.UUID;

/**
 * @Description TODO
 * @Author 奶茶
 * Created by on 2018/10/8 8:52.
 */
@Service
public class SocketTakePhoto implements SocketServerBusinessProcess {

    @Autowired
    private ExamPhotoMapper examPhotoMapper;
    @Autowired
    private ExamineeMapper examineeMapper;
    @Autowired
    private FaceCompareByFile faceCompareByFile;

    @Value("${FaceCompareSocre}")
    private Double faceCompareSocre;

    private static final Logger logger = LoggerFactory.getLogger(SocketTakePhoto.class);

    @Override
    public Object socketServerBusinessProcess(Map<String, Object> requestBody){
        logger.info("-------------------服务端拍照-------------------");
        Integer status = 0;
        OperationResult operationResult;
        //计划ID
        String examPlanID = requestBody.get("examPlanID").toString();
        //考点ID
        String examSiteID = requestBody.get("examSiteID").toString();
        //考生ID
        String examinerID = requestBody.get("examinerID").toString();
        //考生编排ID
        String examinerArrangeID = requestBody.get("examinerArrangeID").toString();
        //考生照片base64
        String data = requestBody.get("data").toString();

        //上传考试照片
        Integer type = PhotoResourceType.Scene.getPrefix();
        StringBuilder basePath = new StringBuilder(ExamSitePath.rootPath);
        StringBuilder filePath = new StringBuilder();
        filePath.append("/").append(examPlanID).append("/").append(examSiteID).append("/photos/").append(examinerID).append("_").append(type).append("_").append(System.currentTimeMillis()).append(".jpg");

        FileUtil.writePhoto(basePath.append(filePath).toString(),data);

        //人脸比对
        Examinee examineeInfo = examineeMapper.findExamineeInfo(examinerID);
        //原照片
        File photoResource = new File(examineeInfo.getPhotoResource());
        //对比照片
        File photoCompare = new File(basePath.append(filePath).toString());
        Double score = Double.valueOf("0");
        int faceCompareResult = 0; // 对比结果  0-false;1-true
        if(photoResource.exists() && photoCompare.exists()){
            score = faceCompareByFile.faceCompareByFile(photoResource, photoCompare);
            if(score.compareTo(faceCompareSocre) > 0){
                faceCompareResult = 1;
            }
        }

        //4,存储
        ExamPhoto examPhoto = new ExamPhoto();
        examPhoto.setExamPhotoID(UUID.randomUUID().toString());
        examPhoto.setExamineeArrangeID(examinerArrangeID);
        examPhoto.setPhotoResourceType(type);
        examPhoto.setPhotoResourcePath(filePath.toString());
        examPhoto.setScore(score);
        examPhoto.setFaceCompareResult(faceCompareResult);//0-false;1-true
        examPhoto.setInsertTime(DateUtil.convertEncryptDateToString(new Date()));
        status = examPhotoMapper.saveExamineePhoto(examPhoto);

        logger.info("抓怕照片信息：" + GsonUtil.toJson(examPhoto));

        if(!status.equals(0)){
            operationResult = new OperationResult(OperationResultType.Success ,OperationResultType.Success.getDisplay(),faceCompareResult);
        }else{
            operationResult = new OperationResult(OperationResultType.Error ,OperationResultType.Error.getDisplay(),null);
        }

        return operationResult;

    }

}
